package cn.lh.util;

import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.X25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.X25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.X25519PublicKeyParameters;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;

public class Curve25519EncryptionDemo {

        public static void main(String[] args) {
            // 添加BouncyCastle作为提供者
            Security.addProvider(new BouncyCastleProvider());

            try {
                // 生成密钥对
                X25519KeyPairGenerator keyPairGenerator = new X25519KeyPairGenerator();
                keyPairGenerator.init(new X25519KeyGenerationParameters(null));
                AsymmetricKeyParameter keyPair = keyPairGenerator.generateKeyPair().getPrivate();

                // 获取私钥和公钥
                X25519PrivateKeyParameters privateKey = (X25519PrivateKeyParameters) keyPair;
                X25519PublicKeyParameters publicKey = privateKey.generatePublicKey();

                // 将私钥和公钥转换为字符串格式
                String privateKeyStr = privateKey.getEncoded().toString();
                String publicKeyStr = publicKey.getEncoded().toString();

                System.out.println("私钥: " + privateKeyStr);
                System.out.println("公钥: " + publicKeyStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

}
